#[[
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
]]

cmake_minimum_required(VERSION 3.7.2)

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_SYSROOT "${CMAKE_BINARY_DIR}")

set(cross_prefix @cross_prefix@)
set(arch @KernelArch@)


if(NOT "${cross_prefix}" STREQUAL "")
    set(CROSS_COMPILER_PREFIX ${cross_prefix})
endif()

if("${CROSS_COMPILER_PREFIX}" STREQUAL "")
    if("${arch}" STREQUAL "arm64") 
        set(CROSS_COMPILER_PREFIX "aarch64-linux-gnu-")
        find_program(res "${CROSS_COMPILER_PREFIX}gcc")
        if (NOT ${res} STREQUAL "res-NOTFOUND")
            message("Found GCC with prefix ${CROSS_COMPILER_PREFIX}")
        else ()
            message(FATAL_ERROR "Unable to find valid cross-compiling GCC: ${CROSS_COMPILER_PREFIX}gcc")
        endif()
    else()
            message(FATAL_ERROR "gcc.cmake does not support this architecture")
    endif()
endif()


set(CMAKE_C_COMPILER ${CROSS_COMPILER_PREFIX}gcc)
set(CMAKE_ASM_COMPILER ${CROSS_COMPILER_PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILER_PREFIX}g++)
set(CMAKE_OBJCOPY ${CROSS_COMPILER_PREFIX}objcopy)


add_compile_options(-fasm -ffreestanding -ffunction-sections -fdata-sections -Wno-unused-function)
#add_compile_options(-g)
#add_compile_options(-pedantic)

add_link_options(-nostdlib)

